package cn.lf.android.lawfirm.undertake.approval.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseFragment;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.approval.UndertakeActivity;
import cn.lf.android.lawfirm.undertake.approval.controler.UnderListConroller;
import cn.lf.android.lawfirm.undertake.approval.view.UndertakeAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;

/**
 * @version time：2017-6-26上午10:09:07
 *          TODO  UndertakeFragment
 */
public class UndertakeFragment extends BaseFragment implements UnderListConroller.UnderListCallback{

    //启动类型
    public static final int TYPE_ALL = 0;
    public static final int TYPE_NO_START = 1;
    public static final int TYPE_START = 2;
    public static final int TYPE_END = 3;


    ListView lv;

    private int type;
    private List<LawCase> data;

    private UndertakeAdapter adapter;

    private SmartRefreshLayout refreshLayout;
    private UnderListConroller mUnderListConroller;
    private int pageIndex = 1;
    /**
     * 待审批（notYet）、已审批（pass）、已退回（sendBack）和全部（all）（必填默认为待审批）（必填）
     **/
    private String state = "all";

    //网络错误页面
//    private View httpErrorV, unNetWorkV, loadFailV;

//    private MarketingActivityController controller;


    @IntDef({TYPE_ALL,TYPE_NO_START, TYPE_START, TYPE_END})
    public @interface StartType {
    }

    public static UndertakeFragment newInstance(@StartType int type) {
        UndertakeFragment turntableFragment = new UndertakeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        turntableFragment.setArguments(bundle);
        return turntableFragment;

    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_undertakelist;
    }

    @Override
    protected void initFragment() {
        lv = getView(R.id.lv_list);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        //设置 Header 为 Material风格
        refreshLayout.setRefreshHeader(new MaterialHeader(getActivity()).setShowBezierWave(true));
        //设置 Footer 为 球脉冲
        refreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));

        type = getArguments().getInt("type");
        data = new ArrayList<LawCase>();
        adapter = new UndertakeAdapter( getActivity(), data,type);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(onItemClickListener);
        mUnderListConroller = new UnderListConroller(this, getActivity());
        mUnderListConroller.onUnderList("",String.valueOf(pageIndex), state);
        setLisenters();
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(2000);
                getType();
                pageIndex = 1;
                data.clear();
                mUnderListConroller.onUnderList("",String.valueOf(pageIndex), state);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
//                refreshlayout.finishLoadmore(2000);
                getType();
                pageIndex++;
                mUnderListConroller.onUnderList("",String.valueOf(pageIndex), state);
            }
        });
    }

    private void getType() {
        switch (type){
            case TYPE_ALL:
                state = "all";
                DebugLog.e("-----all-----");
                break;
            case TYPE_NO_START:
                state = "notYet";
                DebugLog.e("-----notYet-----");
                break;
            case TYPE_START:
                state = "pass";
                DebugLog.e("-----pass-----");
                break;
            case TYPE_END:
                state = "sendBack";
                DebugLog.e("-----sendBack-----");
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    //条目点击事件
    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            DebugLog.e("------点击了item------");
            Intent intent = new Intent(getContext(), UndertakeActivity.class);
            startActivity(intent);
        }
    };

    /**
     * 请求成功的回调
     * @param beans
     */
    @Override
    public void onUnderListuccessed(List<LawCase> beans) {
        DebugLog.e("--------请求成功------");
        for (int i = 0 ;i<=10;i++){
            LawCase lawCase = new LawCase();
            lawCase.createUser = "createUser"+i;
            lawCase.caseID = ""+i;
            lawCase.cognizanceNum = "cognizanceNum"+i;
            lawCase.members = "members"+i;
            lawCase.state = "state"+i;
            lawCase.num = "num"+i;
            data.add(lawCase);
        }
        adapter.notifyDataSetChanged();
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }

    /**
     * 请求失败的回调
     */
    @Override
    public void onUnderListFailed() {
        DebugLog.e("------请求失败------");
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }
}
